<?php

ini_set('date.timezone', 'Asia/Shanghai');

require "weixin/lib/WxPay.NativePay.php";
require 'weixin/lib/WxPay.Notify.php';

/**
 * Description of native
 *
 * @author libaoling
 * @time 2015-4-10
 */
class wxpay extends WxPayNotify
{

    private $data;

    public function get_code($order)
    {
        $notify = new NativePay();
        $input = new WxPayUnifiedOrder();

        /* 商品价格（包含运费），以分为单位 */
        $total_fee = floatval($order['trans_amount']) * 100;

        $input->SetBody('万菱家饰网订单');
        $input->SetOut_trade_no($order['trans_code']);
        $input->SetTotal_fee($total_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetNotify_url('http://www.wljiashi.com/weixin/index.php');
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id($order['trans_code']);
        $result = $notify->GetPayUrl($input);

        return $result;
    }

    public function respond()
    {
        if($this->get_data())
        {
            $status = $this->NotifyProcess($this->data, $msg);
            if($status == 1)
            {
                $this->SetReturn_code("SUCCESS");
                $this->SetReturn_msg("OK");
            }
            else
            {
                $this->SetReturn_code("FAIL");
                $this->SetReturn_msg($msg);
            }

            return $status;
        }
        else
        {
            return -1;
        }
    }

    public function get_out_trade_no()
    {
        return $this->data['out_trade_no'];
    }

    public function check_total_fee($total_price)
    {
        $order_amount = $this->data['total_fee'] / 100;
        if($total_price == $order_amount)
            return true;
        else
            return false;
    }

    public function get_total_fee()
    {
        $order_amount = $this->data['total_fee'] / 100;

        return $order_amount;
    }
    
    function get_pay_rate($money)
    {
            $pay_rate = 0.006;
            return $pay_rate;

    }

    public function error_message($num)
    {
        switch($num)
        {
            case -1:
                $msg = '签名不对或xml数据异常';
                break;
            case -2:
                $msg = '输入参数不正确';
                break;
            case -3:
                $msg = '订单查询失败';
                break;
        }
        return $msg;
    }

    public function verify_result($result)
    {
        if($result){
            WxpayApi::replyNotify($this->ToXml());
        }
    }

    //查询订单
    public function Queryorder($transaction_id)
    {
        $input = new WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = WxPayApi::orderQuery($input);
        if(array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS")
        {
            return true;
        }
        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        if(!array_key_exists("transaction_id", $data))
        {
            $msg = '输入参数不正确';
            return -2;
        }
        //查询订单，判断订单真实性
        if(!$this->Queryorder($data["transaction_id"]))
        {
            $msg = '订单查询失败';
            return -3;
        }
        return 1;
    }

    private function get_data()
    {
        //获取通知的数据
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        //如果返回成功则验证签名
        try
        {
            $data = WxPayResults::Init($xml);
        }
        catch(WxPayException $e)
        {
            $msg = $e->errorMessage();
            return false;
        }
        $this->data = $data;
        return true;
    }

}
